home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2001 May / SGI IRIX Base Documentation 2001 May.iso / usr / share / catman / u_man / cat1 / pstruct.z / pstruct
Encoding:
Text File  |  1998-10-30  |  11.8 KB  |  331 lines

  1.  
  2.  
  3.  
  4. PPPPSSSSTTTTRRRRUUUUCCCCTTTT((((1111))))                                                          PPPPSSSSTTTTRRRRUUUUCCCCTTTT((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      c2ph, pstruct - Dump C structures as generated from cc -g -S stabs
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.          c2ph [-dpnP] [var=val] [files ...]
  13.  
  14.  
  15.      OOOOPPPPTTTTIIIIOOOONNNNSSSS
  16.  
  17.          Options:
  18.  
  19.          -w  wide; short for: type_width=45 member_width=35 offset_width=8
  20.          -x  hex; short for:  offset_fmt=x offset_width=08 size_fmt=x size_width=04
  21.  
  22.          -n  do not generate perl code  (default when invoked as pstruct)
  23.          -p  generate perl code         (default when invoked as c2ph)
  24.          -v  generate perl code, with C decls as comments
  25.  
  26.          -i  do NOT recompute sizes for intrinsic datatypes
  27.          -a  dump information on intrinsics also
  28.  
  29.          -t  trace execution
  30.          -d  spew reams of debugging output
  31.  
  32.          -slist  give comma-separated list a structures to dump
  33.  
  34.  
  35. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  36.      The following is the old c2ph.doc documentation by Tom Christiansen
  37.      <tchrist@perl.com> Date: 25 Jul 91 08:10:21 GMT
  38.  
  39.      Once upon a time, I wrote a program called pstruct.  It was a perl
  40.      program that tried to parse out C structures and display their member
  41.      offsets for you.  This was especially useful for people looking at binary
  42.      dumps or poking around the kernel.
  43.  
  44.      Pstruct was not a pretty program.  Neither was it particularly robust.
  45.      The problem, you see, was that the C compiler was much better at parsing
  46.      C than I could ever hope to be.
  47.  
  48.      So I got smart:  I decided to be lazy and let the C compiler parse the C,
  49.      which would spit out debugger stabs for me to read.  These were much
  50.      easier to parse.  It's still not a pretty program, but at least it's more
  51.      robust.
  52.  
  53.      Pstruct takes any .c or .h files, or preferably .s ones, since that's the
  54.      format it is going to massage them into anyway, and spits out listings
  55.      like this:
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPSSSSTTTTRRRRUUUUCCCCTTTT((((1111))))                                                          PPPPSSSSTTTTRRRRUUUUCCCCTTTT((((1111))))
  71.  
  72.  
  73.  
  74.       struct tty {
  75.         int                          tty.t_locker                         000      4
  76.         int                          tty.t_mutex_index                    004      4
  77.         struct tty *                 tty.t_tp_virt                        008      4
  78.         struct clist                 tty.t_rawq                           00c     20
  79.           int                        tty.t_rawq.c_cc                      00c      4
  80.           int                        tty.t_rawq.c_cmax                    010      4
  81.           int                        tty.t_rawq.c_cfx                     014      4
  82.           int                        tty.t_rawq.c_clx                     018      4
  83.           struct tty *               tty.t_rawq.c_tp_cpu                  01c      4
  84.           struct tty *               tty.t_rawq.c_tp_iop                  020      4
  85.           unsigned char *            tty.t_rawq.c_buf_cpu                 024      4
  86.           unsigned char *            tty.t_rawq.c_buf_iop                 028      4
  87.         struct clist                 tty.t_canq                           02c     20
  88.           int                        tty.t_canq.c_cc                      02c      4
  89.           int                        tty.t_canq.c_cmax                    030      4
  90.           int                        tty.t_canq.c_cfx                     034      4
  91.           int                        tty.t_canq.c_clx                     038      4
  92.           struct tty *               tty.t_canq.c_tp_cpu                  03c      4
  93.           struct tty *               tty.t_canq.c_tp_iop                  040      4
  94.           unsigned char *            tty.t_canq.c_buf_cpu                 044      4
  95.           unsigned char *            tty.t_canq.c_buf_iop                 048      4
  96.         struct clist                 tty.t_outq                           04c     20
  97.           int                        tty.t_outq.c_cc                      04c      4
  98.           int                        tty.t_outq.c_cmax                    050      4
  99.           int                        tty.t_outq.c_cfx                     054      4
  100.           int                        tty.t_outq.c_clx                     058      4
  101.           struct tty *               tty.t_outq.c_tp_cpu                  05c      4
  102.           struct tty *               tty.t_outq.c_tp_iop                  060      4
  103.           unsigned char *            tty.t_outq.c_buf_cpu                 064      4
  104.           unsigned char *            tty.t_outq.c_buf_iop                 068      4
  105.         (*int)()                     tty.t_oproc_cpu                      06c      4
  106.         (*int)()                     tty.t_oproc_iop                      070      4
  107.         (*int)()                     tty.t_stopproc_cpu                   074      4
  108.         (*int)()                     tty.t_stopproc_iop                   078      4
  109.         struct thread *              tty.t_rsel                           07c      4
  110.  
  111.      etc.
  112.  
  113.      Actually, this was generated by a particular set of options.  You can
  114.      control the formatting of each column, whether you prefer wide or fat,
  115.      hex or decimal, leading zeroes or whatever.
  116.  
  117.      All you need to be able to use this is a C compiler than generates
  118.      BSD/GCC-style stabs.  The ----gggg option on native BSD compilers and GCC
  119.      should get this for you.
  120.  
  121.      To learn more, just type a bogus option, like ----\\\\????, and a long usage
  122.      message will be provided.  There are a fair number of possibilities.
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPSSSSTTTTRRRRUUUUCCCCTTTT((((1111))))                                                          PPPPSSSSTTTTRRRRUUUUCCCCTTTT((((1111))))
  137.  
  138.  
  139.  
  140.      If you're only a C programmer, than this is the end of the message for
  141.      you.  You can quit right now, and if you care to, save off the source and
  142.      run it when you feel like it.  Or not.
  143.  
  144.      But if you're a perl programmer, then for you I have something much more
  145.      wondrous than just a structure offset printer.
  146.  
  147.      You see, if you call pstruct by its other incybernation, c2ph, you have a
  148.      code generator that translates C code into perl code!  Well, structure
  149.      and union declarations at least, but that's quite a bit.
  150.  
  151.      Prior to this point, anyone programming in perl who wanted to interact
  152.      with C programs, like the kernel, was forced to guess the layouts of the
  153.      C strutures, and then hardwire these into his program.  Of course, when
  154.      you took your wonderfully crafted program to a system where the sgtty
  155.      structure was laid out differently, you program broke.  Which is a shame.
  156.  
  157.      We've had Larry's h2ph translator, which helped, but that only works on
  158.      cpp symbols, not real C, which was also very much needed.  What I offer
  159.      you is a symbolic way of getting at all the C structures.  I've couched
  160.      them in terms of packages and functions.  Consider the following program:
  161.  
  162.          #!/usr/local/bin/perl
  163.  
  164.          require 'syscall.ph';
  165.          require 'sys/time.ph';
  166.          require 'sys/resource.ph';
  167.  
  168.          $ru = "\0" x &rusage'sizeof();
  169.  
  170.          syscall(&SYS_getrusage, &RUSAGE_SELF, $ru)      && die "getrusage: $!";
  171.  
  172.          @ru = unpack($t = &rusage'typedef(), $ru);
  173.  
  174.          $utime =  $ru[ &rusage'ru_utime + &timeval'tv_sec  ]
  175.                 + ($ru[ &rusage'ru_utime + &timeval'tv_usec ]) / 1e6;
  176.  
  177.          $stime =  $ru[ &rusage'ru_stime + &timeval'tv_sec  ]
  178.                 + ($ru[ &rusage'ru_stime + &timeval'tv_usec ]) / 1e6;
  179.  
  180.          printf "you have used %8.3fs+%8.3fu seconds.\n", $utime, $stime;
  181.  
  182.      As you see, the name of the package is the name of the structure.
  183.      Regular fields are just their own names.  Plus the following accessor
  184.      functions are provided for your convenience:
  185.  
  186.          struct      This takes no arguments, and is merely the number of first-level
  187.                      elements in the structure.  You would use this for indexing
  188.                      into arrays of structures, perhaps like this
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPSSSSTTTTRRRRUUUUCCCCTTTT((((1111))))                                                          PPPPSSSSTTTTRRRRUUUUCCCCTTTT((((1111))))
  203.  
  204.  
  205.  
  206.                          $usec = $u[ &user'u_utimer
  207.                                      + (&ITIMER_VIRTUAL * &itimerval'struct)
  208.                                      + &itimerval'it_value
  209.                                      + &timeval'tv_usec
  210.                                    ];
  211.  
  212.          sizeof      Returns the bytes in the structure, or the member if
  213.                      you pass it an argument, such as
  214.  
  215.                              &rusage'sizeof(&rusage'ru_utime)
  216.  
  217.          typedef     This is the perl format definition for passing to pack and
  218.                      unpack.  If you ask for the typedef of a nothing, you get
  219.                      the whole structure, otherwise you get that of the member
  220.                      you ask for.  Padding is taken care of, as is the magic to
  221.                      guarantee that a union is unpacked into all its aliases.
  222.                      Bitfields are not quite yet supported however.
  223.  
  224.          offsetof    This function is the byte offset into the array of that
  225.                      member.  You may wish to use this for indexing directly
  226.                      into the packed structure with vec() if you're too lazy
  227.                      to unpack it.
  228.  
  229.          typeof      Not to be confused with the typedef accessor function, this
  230.                      one returns the C type of that field.  This would allow
  231.                      you to print out a nice structured pretty print of some
  232.                      structure without knoning anything about it beforehand.
  233.                      No args to this one is a noop.  Someday I'll post such
  234.                      a thing to dump out your u structure for you.
  235.  
  236.      The way I see this being used is like basically this:
  237.  
  238.              % h2ph <some_include_file.h  >  /usr/lib/perl/tmp.ph
  239.              % c2ph  some_include_file.h  >> /usr/lib/perl/tmp.ph
  240.              % install
  241.  
  242.      It's a little tricker with c2ph because you have to get the includes
  243.      right.  I can't know this for your system, but it's not usually too
  244.      terribly difficult.
  245.  
  246.      The code isn't pretty as I mentioned  -- I never thought it would be a
  247.      1000- line program when I started, or I might not have begun. :-)  But I
  248.      would have been less cavalier in how the parts of the program
  249.      communicated with each other, etc.  It might also have helped if I didn't
  250.      have to divine the makeup of the stabs on the fly, and then account for
  251.      micro differences between my compiler and gcc.
  252.  
  253.      Anyway, here it is.  Should run on perl v4 or greater.  Maybe less.
  254.  
  255.       --tom
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPSSSSTTTTRRRRUUUUCCCCTTTT((((1111))))                                                          PPPPSSSSTTTTRRRRUUUUCCCCTTTT((((1111))))
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.                                                                         PPPPaaaaggggeeee 5555
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.